﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

namespace DreamBox.Admin.Controls
{
    public partial class PagerUserControl : System.Web.UI.UserControl
    {

        DAL.Formpage formPage = new DAL.Formpage();
        //分页所需传过来的参数
        private string _tableName;
        /// <summary>
        /// 表名
        /// </summary>
        public string TableName
        {
            get { return _tableName; }
            set { _tableName = value; }
        }
        /// <summary>
        /// 排序字段
        /// </summary>
        private string _fldName;

        public string FldName
        {
            get { return _fldName; }
            set { _fldName = value; }
        }
        /// <summary>
        /// 页码大小
        /// </summary>
        private int _PageSize;

        public int PageSize
        {
            get { return _PageSize; }
            set { _PageSize = value; }
        }
        /// <summary>
        /// 数据绑定控件
        /// </summary>
        private Repeater _PagerControls;

        public Repeater PagerControls
        {
            get { return _PagerControls; }
            set { _PagerControls = value; }
        }
        /// <summary>
        /// 分页条件
        /// </summary>
        private string _where;

        public string Where
        {
            get { return _where; }
            set { _where = value; }
        }
        /// <summary>
        /// 排序类型(0 升序,1 降序)
        /// </summary>
        private int _Order;

        public int Order
        {
            get { return _Order; }
            set { _Order = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindPage(1);
            }
             
        }
        //绑定到控件上
        void BindView(int PageSize, int PageIndex)
        {
            PagerControls.DataSource = formPage.Order(TableName, FldName, PageSize, PageIndex, Where, Order);
            PagerControls.DataBind(); 
        }
        //绑定相关数据
        public void BindPage(int PageIndex)
        {

            //得到指定表的记录数量
            string where = "";
            if (Where != "")
            {
                where = Where;
            }


            int RowNum = int.Parse(formPage.Total(TableName, FldName, PageSize, PageIndex, Where).Rows[0]["total"].ToString());

            RowCount.Text = RowNum.ToString();
            int PageCount = 0;
            //取得一共有多少页
            if (RowNum % PageSize == 0) 
            {
                PageCount = RowNum / PageSize;
            }
            else
            {
                PageCount = RowNum / PageSize + 1;
            }
            //判断上一页是否超出范围
            if (PageIndex == 1)
            {
                Index.Enabled = false;
                OnIndex.Enabled = false;
            }
            else
            {
                Index.Enabled = true;
                OnIndex.Enabled = true;
            }
            //判断下一页是否超出范围
            if (PageIndex == PageCount)
            {
                FinallyIndex.Enabled = false;
                UnderIndex.Enabled = false;
            }
            else
            {
                FinallyIndex.Enabled = true;
                UnderIndex.Enabled = true;
            }
            ViewState["pagecount"] = PageCount.ToString();
            if (PageCount != 0)
            {
                PeIndex.Text = PageIndex.ToString();
            }
            else
            {
                PeIndex.Text = "0";
            }
            PageRowCount.Text = PageCount.ToString();

            BindView(PageSize, PageIndex);

            
        }
        //首页
        protected void Index_Click(object sender, EventArgs e)
        {
            BindPage(1);
        }
        //上一页
        protected void OnIndex_Click(object sender, EventArgs e)
        {
            BindPage(int.Parse(PeIndex.Text) - 1);
        }
        //下一页
        protected void UnderIndex_Click(object sender, EventArgs e)
        {
            BindPage(int.Parse(PeIndex.Text) + 1);
        }
        //最后一页
        protected void FinallyIndex_Click(object sender, EventArgs e)
        {
            BindPage(int.Parse(PageRowCount.Text));
        }
        ////跳转
        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    Regex reg = new Regex(@"^[1-9]\d*$");
        //    Match ma = reg.Match(TurnIndex.Text);
        //    int PageCount = int.Parse(ViewState["pagecount"].ToString());
        //    if (ma.Success)
        //    {
        //        int PageIndex = int.Parse(TurnIndex.Text);
        //        if (PageIndex < 1 || PageIndex > PageCount)
        //        {
        //            BLL.Admin.Tips.JsTips(this.Page, "您输入的页码超出范围");
        //        }
        //        else
        //        {
        //            BindPage(int.Parse(TurnIndex.Text));
        //        }
        //    }
        //    else
        //    {
        //        BLL.Admin.Tips.JsTips(this.Page, "请输入正确的页码");
        //    }
        //}
    }
}